# -*-coding:utf-8-*-
# -*-coding:utf-8-*-
import re
from utils import read_txt,cut_sent,read_word,read_hed,read_pos
from Syntactic_Analysis import ParseResult

'''
判断“是”字句
LAC默认模型分词 粒度太大 如：我没有钱---> 我没有  钱
换用北大分词工具 pkuseg LAC标注词性
分析"是"字句的句法结构
type_lst = ['主||“是”+名/代/名词词组', '主||“是”+数量词', '主||“是”+形/形容词词组', '主||“是”+动/动词词组', 
            '主||“是”+主谓词组', '主||“是”+介词词组', '主||“是”+“的”字词组',  '非“是”字句]
两种情形：
1. HED为“是”  确定修饰HED的SBV和VOB     
2. HED为其他动词或者形容词  确定作为ADV或VOB的修饰HED的“是”     
7.2 更新 精简规则
'''

def find_key(sentence):
    # 正则表达式寻找关键字 确定句子中含“是”
    pattern_1 = re.compile(r'[\u662f]')  # 是
    if re.findall(pattern_1, sentence):
        return sentence
    return ""


def judge_type(hed_info, modifi_info):
    sbv_info = modifi_info['sbv_info']  # SBV成分
    vob_info = modifi_info['vob_info']  # VOB成分
    adv_info = modifi_info['adv_info']  # ADV成分
    att_info = modifi_info['att_info']  # ATT成分
    pob_info = modifi_info['pob_info']  # POB成分
    sbv_h = read_hed(sbv_info, hed_info['word'])  # 修饰HED的SBV成分
    vob_h = read_hed(vob_info, hed_info['word'])  # 修饰HED的VOB成分
    adv_h = read_hed(adv_info, hed_info['word'])  # 修饰HED的ADV成分


    type_lst = ['主||“是”+名/代/名词词组', '主||“是”+数量词', '主||“是”+形/形容词词组', '主||“是”+动/动词词组',
            '主||“是”+主谓词组', '主||“是”+介词词组', '主||“是”+“的”字词组', '其他句型']

    noun_lst = ['n', 'f', 's', 't', 'nr', 'ns', 'nt', 'nw', 'nz', 'r']
    verb_lst = ['v', 'vd', 'vn']
    adj_lst = ['a', 'ad', 'an']
    quan_lst = ['m', 'q']
    type_id = 7

    # 第一种情形 “是”作为HED 寻找修饰“是”的SBV或VOB
    if (find_key(hed_info['word'])) and sbv_h:
        # 注意判断顺序 避免规则冲突 越宽泛的条件，放到越后面
        # '主||“是”+“的”字词组'
        if read_word(vob_info,['的']):      # ……的  词组做宾语
            type_id = 6
            return type_lst[type_id]
        # '主||“是”+数量词'
        if read_pos(att_info,quan_lst) :
            type_id = 1
            return type_lst[type_id]
        # '主||“是”+主谓词组'
        if read_hed(sbv_info,vob_h['word']):      # 宾语是否为主谓结构
            type_id = 4
            return type_lst[type_id]
        #  '主||“是”+介词词组'
        if read_hed(pob_info,vob_h['word']) and vob_h['pos'] == 'p':      # 宾语是否为介宾结构
            type_id = 5
            return type_lst[type_id]
        # 主||“是”+形/形容词词组
        if vob_h and vob_h['pos'] in adj_lst:       # 宾语是否为形容词
            type_id = 2
            return type_lst[type_id]
        # 主||“是”+动/动词词组
        if vob_h and vob_h['pos'] in verb_lst:       # 宾语是否为动词
            type_id = 3
            return type_lst[type_id]
        # 主||“是”+名/代/名词词组
        if vob_h and vob_h['pos'] in noun_lst:       # 宾语是否为名词
            type_id = 0
            return type_lst[type_id]

    # 第二种情形 其他动词或形容词作为HED  寻找作为ADV的”是“
    elif adv_h and find_key(adv_h['word']):
        # 主 | |“是”+形 / 形容词词组
        if hed_info['pos'] in adj_lst:
            type_id = 2
            return type_lst[type_id]
        # 主||“是”+动/动词词组
        if hed_info['pos'] in verb_lst:
            type_id = 3
            return type_lst[type_id]
    return type_lst[type_id]


if __name__ == '__main__':
    path = 'data/test_shi.txt'
    sentences = read_txt(path)
    for sent in sentences:
        print('*' * 125)
        print(sent)
        fenci_res, pos_res, ddp_res = cut_sent(sent)
        pr = ParseResult(fenci_res, pos_res, ddp_res)
        print('分词：', fenci_res)
        print('词性：', pos_res)
        print('句法：', ddp_res)
        modifi_info = pr.get_modifi_info()
        hed_info = pr.get_hed_info()
        print(judge_type(hed_info, modifi_info))
